package com.hrt.datasender.datacollect.drive.ttmb;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.btcode.common.BitType;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datadao.model.BcDeviceConfigModel;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.serial.modbus.ModbusDataDecodeTTMB;
import com.hrt.serial.modbus.ModbusDataModel;

public class TTMBDrive extends TTMBSerialBaseDrive {
    
    private final int preciTrans10 = 10;
    
    private final int preciTrans100 = 100;

    private ModbusDataDecodeTTMB decode = new ModbusDataDecodeTTMB();

    @Override
    public List<BdSourceModel> processSerialData(byte[] data, HashMap<Integer, String> paramCodeMap,
            DriveConfigModel configs) {
        HashMap deviceConfig = configs.getConfig();
        deviceConfig.put("dataBit","16");
        configs.setConfig(deviceConfig);
        List<ModbusDataModel> modbusDataModels = decode.dataModelDecode(data, data.length, configs);
        ModbusDataModel modbusDataModel = modbusDataModels.get(0);
        double[] doubleData = modbusDataModel.getData();

        List<BdSourceModel> sourceModels = new ArrayList<>();

        for (int i = 0; i < doubleData.length; i++) {

            String paramCodeBus = paramCodeMap.get(i);

            if (paramCodeBus == null) {
                continue;
            }
            BdSourceModel sourModel = new BdSourceModel();
            sourModel.setParamCodeBus(paramCodeBus);
            sourModel.setDatatime(new Date());
            // 气压仪器精度问题，风速、紫外辐射要除以10倍、气压除以100倍
            if (paramCodeBus.equals("a01007") || paramCodeBus.equals("ZWFS")) {
                sourModel.setMeasRe(doubleData[i] / preciTrans10);
            }
            else if (paramCodeBus.equals("a01006")) {
                sourModel.setMeasRe(doubleData[i] / preciTrans100);
            }
            else {
                sourModel.setMeasRe(doubleData[i]);
            }
            sourceModels.add(sourModel);
        }

        return sourceModels;
    }

    // public double[] decodeTTMB(byte[] data){
    // double[] doubleData;
    //
    // return doubleData;
    // }

    @Override
    public String getNote() {
        return null;
    }

    @Override
    public String getDriveName() {
        return "气象仪TTMB协议";
    }

    @Override
    public String getConsStr() {
        return "0/10 * * * * ?";
    }

    @Override
    protected void beforeInit(BcDevicesModel device, List<BcDeviceMonitorParamModel> params,
            List<BcDeviceConfigModel> configs) {
        // TODO Auto-generated method stub

    }
}